<?php

/**
 * PhoneNumber filter form base class.
 *
 * @package    Wildlive
 * @subpackage filter
 * @author     Maximilian Lenkeit <dev@lenki.com>
 * @version    SVN: $Id: sfDoctrineFormFilterGeneratedTemplate.php 29570 2010-05-21 14:49:47Z Kris.Wallsmith $
 */
abstract class BasePhoneNumberFormFilter extends BaseFormFilterDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'area_code'    => new sfWidgetFormFilterInput(),
      'call_through' => new sfWidgetFormFilterInput(),
      'type'         => new sfWidgetFormChoice(array('choices' => array('' => '', 'landline; fax; mobile' => 'landline; fax; mobile'))),
      'country_id'   => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Country'), 'add_empty' => true)),
      'member_list'  => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'PersonalMemberData')),
    ));

    $this->setValidators(array(
      'area_code'    => new sfValidatorPass(array('required' => false)),
      'call_through' => new sfValidatorPass(array('required' => false)),
      'type'         => new sfValidatorChoice(array('required' => false, 'choices' => array('landline; fax; mobile' => 'landline; fax; mobile'))),
      'country_id'   => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('Country'), 'column' => 'id')),
      'member_list'  => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'PersonalMemberData', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('phone_number_filters[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function addMemberListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.MemberPhoneNumber MemberPhoneNumber')
      ->andWhereIn('MemberPhoneNumber.member_id', $values)
    ;
  }

  public function getModelName()
  {
    return 'PhoneNumber';
  }

  public function getFields()
  {
    return array(
      'id'           => 'Number',
      'area_code'    => 'Text',
      'call_through' => 'Text',
      'type'         => 'Enum',
      'country_id'   => 'ForeignKey',
      'member_list'  => 'ManyKey',
    );
  }
}
